<template>
  <div>
    <x-card v-if="hasPerm('gardenhouse:page')">
      <div
        slot="content"
        class="table-page-search-wrapper"
      >
        <a-form layout="inline">
          <a-row :gutter="20">
            <a-col
              :md="8"
              :sm="24"
            >
              <a-form-item label="教室名称">
                <a-input
                  v-model="queryParam.CRoomName"
                  allow-clear
                  placeholder="请输入教室名称"
                />
              </a-form-item>
            </a-col>
            <a-col
              :md="8"
              :sm="24"
            >
              <a-button
                type="primary"
                @click="$refs.table.refresh(true)"
              >查询</a-button>
            </a-col>
          </a-row>
        </a-form>
      </div>
    </x-card>
    <a-card :bordered="false">
      <s-table
        ref="table"
        :columns="columns"
        :data="loadData"
        :alert="options.alert"
        :rowKey="(record) => record.id"
        :rowSelection="options.rowSelection"
      >
        <template
          slot="operator"
          v-if="hasPerm('gardenhouse:add')"
        >
          <a-button
            @click="$refs.addForm.add()"
            icon="plus"
            type="primary"
            v-if="hasPerm('gardenhouse:add')"
          >新增教室</a-button>
          <a-button
            type="danger"
            :disabled="selectedRowKeys.length < 1"
            v-if="hasPerm('gardenhouse:delete')"
            @click="batchDelete"
          ><a-icon type="delete" />批量删除</a-button>
          <x-down
            style="display: none"
            v-if="hasPerm('gardenhouse:export')"
            ref="batchExport"
            @batchExport="batchExport"
          />
        </template>
        <span
          slot="cRoomState"
          slot-scope="text, record"
        >
          <span slot="cRoomState">
            <a-switch
              v-model="record.cRoomState"
              @change="changeStatus(record)"
            />
          </span>
        </span>
        <span
          slot="action"
          slot-scope="text, record"
        >
          <a
            v-if="hasPerm('gardenhouse:edit')"
            @click="$refs.editForm.edit(record)"
          >编辑</a>
          <a-divider
            type="vertical"
            v-if="hasPerm('gardenhouse:edit') & hasPerm('gardenhouse:delete')"
          />
          <a-popconfirm
            v-if="hasPerm('gardenhouse:delete')"
            placement="topRight"
            title="确认删除？"
            @confirm="() => singleDelete(record)"
          >
            <a>删除</a>
          </a-popconfirm>
        </span>
      </s-table>
      <add-form
        ref="addForm"
        @ok="handleOk"
      />
      <edit-form
        ref="editForm"
        @ok="handleOk"
      />
    </a-card>
  </div>
</template>

<script>
import { STable, XCard, XDown } from '@/components'
import { sysPosPage, sysPosDelete } from '@/api/modular/garden/posManage'
import addForm from './addForm'
import editForm from './editForm'

export default {
  components: {
    XDown,
    XCard,
    STable,
    addForm,
    editForm,
  },

  data() {
    return {
      // 查询参数
      queryParam: {
        CRoomId: '',
        CRoomName: '',
        CRoomCode: '',
        CRoomState: '',
      },
      // 表头
      columns: [
        {
          title: '教室名称',
          dataIndex: 'cRoomName',
        },
        {
          title: '教室位置',
          dataIndex: 'cRoomCode',
        },
        {
          title: '状态',
          dataIndex: 'cRoomState',
          scopedSlots: { customRender: 'cRoomState' },
        },
      ],
      // 加载数据方法 必须为 Promise 对象
      loadData: (parameter) => {
        return sysPosPage(Object.assign(parameter, this.queryParam)).then((res) => {
          return res.data
        })
      },
      selectedRowKeys: [],
      selectedRows: [],
      options: {
        alert: {
          show: true,
          clear: () => {
            this.selectedRowKeys = []
          },
        },
        rowSelection: {
          selectedRowKeys: this.selectedRowKeys,
          onChange: this.onSelectChange,
        },
      },
    }
  },

  created() {
    if (this.hasPerm('gardenhouse:edit') || this.hasPerm('gardenhouse:delete')) {
      this.columns.push({
        title: '操作',
        width: '150px',
        dataIndex: 'action',
        scopedSlots: { customRender: 'action' },
      })
    }
  },

  methods: {
    /**
     * 单个删除
     */
    singleDelete(record) {
      const param = [{ cRoomId: record.id }]
      this.sysPosDelete(param)
    },
    /**
     * 批量删除
     */
    batchDelete() {
      const paramIds = this.selectedRowKeys.map((d) => {
        return { cRoomId: d }
      })
      this.sysPosDelete(paramIds)
    },
    /**
     * 删除
     */
    sysPosDelete(param) {
      sysPosDelete(param)
        .then((res) => {
          if (res.success) {
            this.$message.success('删除成功')
            this.$refs.table.clearRefreshSelected()
          } else {
            this.$message.error('删除失败：' + res.message)
          }
        })
        .catch((err) => {
          this.$message.error('删除错误：' + err.message)
        })
    },
    changeStatus(record) {
      console.log(record)
    },
    /**
     * 批量导出
     */
    batchExport() {
      // const paramIds = this.selectedRowKeys.map((d) => {
      //   return { 'id': d }
      // })
      // sysPosExport(paramIds).then((res) => {
      //   this.$refs.batchExport.downloadfile(res)
      // })
    },
    handleOk() {
      this.$refs.table.refresh()
    },
    onSelectChange(selectedRowKeys, selectedRows) {
      this.selectedRowKeys = selectedRowKeys
      this.selectedRows = selectedRows
    },
  },
}
</script>

<style lang="less">
.table-operator {
  margin-bottom: 18px;
}
button {
  margin-right: 8px;
}
</style>
